# -*- coding: utf-8 -*-
# @Time : 2021/9/17 14:14
# @Author : Mr  jelis
# @Email : 1402615849@qq.com
# @File : home_page.py
# @Project : mryx_test

"""主页页面类"""
from appium.webdriver.common.mobileby import MobileBy as By
from appium.webdriver.common.mobileby import MobileBy
from page.base_page import BasePage
from utils.swipe import Swipe
class HomePage(BasePage):
    """定义定位器"""
    mid_autumn_locator = (By.XPATH, '//android.widget.ImageView[@resource-id=\"cn.missfresh.application:id/iv_topview\"]')
    miaosha_locator = (By.XPATH, '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/'
              'android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/'
              'android.widget.RelativeLayout/android.widget.FrameLayout/android.view.ViewGroup/'
              'android.widget.FrameLayout/androidx.recyclerview.widget.RecyclerView/android.widget.RelativeLayout[2]/'
              'androidx.recyclerview.widget.RecyclerView/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/'
              'android.widget.FrameLayout/android.widget.ImageView')
    adress_locator = (By.ID, 'cn.missfresh.application:id/address_tv')  # 定位地址
    search_locator = (By.XPATH, '	/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.'
                                'FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.'
                                'FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.'
                                'RelativeLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.'
                                'LinearLayout/android.widget.FrameLayout/android.widget.TextView')  # 搜索输入框

    rdsc_locator= (By.XPATH, '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/'
                             'android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/'
                             'android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout/'
                             'android.view.View[1]/android.widget.FrameLayout/android.view.View/'
                             'android.widget.RelativeLayout/android.widget.RelativeLayout/android.view.View[1]/'
                             'android.widget.LinearLayout[1]/android.widget.ImageView')  #肉蛋食材


    goods_locator = (MobileBy.XPATH,
                     '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/'
                     'android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.'
                     'RelativeLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/'
                     'androidx.recyclerview.widget.RecyclerView/android.widget.FrameLayout/android.widget.RelativeLayout'
                     '/androidx.recyclerview.widget.RecyclerView/android.widget.RelativeLayout[2]/android.widget.'
                     'FrameLayout/android.widget.ImageView')  #要添加的商品定位器
    goods_name_locator = (MobileBy.XPATH,
                          '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout'
                          '/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.'
                          'widget.RelativeLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.'
                          'FrameLayout/androidx.recyclerview.widget.RecyclerView/android.widget.FrameLayout/android.'
                          'widget.RelativeLayout/androidx.recyclerview.widget.RecyclerView/android.widget.'
                          'RelativeLayout[2]/android.widget.TextView[1]') #商品名称定位器

    shopcat_lcoator = (MobileBy.XPATH,
                       '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout'
                       '/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/'
                       'android.widget.RelativeLayout[4]/android.widget.TextView[1]')  #【购物车】定位器

    goods2_locator = (MobileBy.XPATH,
                      '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/'
                      'android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget'
                      '.RelativeLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/'
                      'androidx.recyclerview.widget.RecyclerView/android.widget.FrameLayout/android.widget.'
                      'RelativeLayout/androidx.recyclerview.widget.RecyclerView/android.widget.RelativeLayout[3]'
                      '/android.widget.FrameLayout/android.widget.ImageView')

    goods3_locator = (MobileBy.XPATH,
                      '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout'
                      '/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.'
                      'widget.RelativeLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.'
                      'FrameLayout/androidx.recyclerview.widget.RecyclerView/android.widget.FrameLayout/android.'
                      'widget.RelativeLayout/androidx.recyclerview.widget.RecyclerView/android.widget.RelativeLayout[4]'
                      '/android.widget.FrameLayout/android.widget.ImageView')
    shopping_locator = (By.ID,'cn.missfresh.application:id/cartTab')  # 真机的购物车定位器
    home_locator = (By.ANDROID_UIAUTOMATOR, 'new UiSelector().text("首页")')  # 首页

    goods_sort_locator = (By.ANDROID_UIAUTOMATOR, 'new UiSelector().text("分类")')  # 首页分类
    point_1_locator=(By.XPATH,'/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/'
                            'android.widget.LinearLayout/'
                            'android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/'
                            'android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/'
                            'androidx.recyclerview.widget.RecyclerView/android.view.ViewGroup/android.widget.RelativeLayout/'
                            'android.widget.LinearLayout/android.widget.LinearLayout/android.widget.RelativeLayout/'
                            'androidx.viewpager.widget.ViewPager/android.widget.ImageView')  #滚动点1
    point_2_locator=(By.XPATH,'/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/'
                              'android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/'
                              'android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout/'
                              'android.view.ViewGroup/android.widget.FrameLayout/androidx.recyclerview.widget.RecyclerView/'
                              'android.widget.FrameLayout[2]/android.widget.RelativeLayout/'
                              'android.widget.ImageView')    #滚动点2
    recommended_locator=(By.ID,'cn.missfresh.application:id/top_image')  #定位口碑推荐
    similar_locator=(By.XPATH,'/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/'
                              'android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/'
                              'android.widget.RelativeLayout/android.widget.FrameLayout/android.view.ViewGroup/'
                              'android.widget.FrameLayout/androidx.recyclerview.widget.RecyclerView/android.widget.RelativeLayout[2]/'
                              'androidx.viewpager.widget.ViewPager/androidx.recyclerview.widget.RecyclerView/'
                              'android.widget.FrameLayout[1]/android.widget.LinearLayout/android.widget.LinearLayout/'
                              'android.widget.LinearLayout/android.widget.TextView')   #查看相似定位器

    def zhongqiu(self):
        """中秋促销活动"""
        self.driver.find_element(*self.mid_autumn_locator).click()
    def miaosha(self):
        """秒杀活动"""
        s = Swipe(self.driver)
        s.up_swipe()
        self.driver.find_element(*self.miaosha_locator).click()
    def click_adress(self):
        """点击定位"""
        self.driver.find_element(*self.adress_locator).click()
    def click_search(self):
        """点击搜索框"""
        self.driver.find_element(*self.search_locator).click()

    def click_recommended(self):
        """点击口碑推荐"""
        self.driver.find_element(*self.recommended_locator).click()
    def drop_down_refresh(self):
        """下拉刷新"""
        start=self.driver.find_element(*self.point_1_locator)
        end=self.driver.find_element(*self.point_2_locator)
        self.driver.scroll(start,end)
    def scroll_up(self):
        """向上滚动"""
        start=self.driver.find_element(*self.point_1_locator)
        end=self.driver.find_element(*self.point_2_locator)
        self.driver.scroll(end,start)
    def click_goods(self,x=231,y=702,duration=5000):
        """选择商品并长按"""
        self.driver.tap([(x,y)],duration)
    def click_similar_locator(self):
        """点击查看相似产品"""
        self.driver.find_element(*self.similar_locator).click()


    def click_rdsc(self):
        """点击肉蛋食材"""
        self.driver.find_element(*self.rdsc_locator).click()

    def add_goods(self):
        '''添加商品到购物车'''
        self.driver.find_element(*self.goods_locator).click()
    def get_name(self):
        '''获取添加的商品名称'''
        name_text=self.driver.find_element(*self.goods_name_locator).text   #获取添加的那个商品名称
        return name_text

    def click_shoppingcat(self):
        '''点击【购物车】'''
        self.driver.find_element(*self.shopcat_lcoator).click()  # 点击【购物车】

    def add_sec_goods(self):
        '''添加第二个商品'''
        self.driver.find_element(*self.goods2_locator).click()  # 添加另一个商品

    def add_thd_goods(self):
        '''添加第三个商品'''
        self.driver.find_element(*self.goods3_locator).click()  # 添加第三个商品

    def click_sort(self):
        """点击分类"""
        self.driver.find_element(*self.goods_sort_locator).click()
    def click_shopping(self):
        """点击真机购物车"""
        self.driver.find_element(*self.shopping_locator).click()
    def click_home(self):
        """点击首页"""
        self.driver.find_element(*self.home_locator).click()